package pl.edu.agh.agents.state;

import java.util.ArrayList;
import java.util.List;

import pl.edu.agh.agents.agent.Agent;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

public class State {
	private int ID;
	@XStreamOmitField
	private Agent agent;
	@XStreamOmitField
	private List<State> neighbors;
	@XStreamOmitField
	private boolean agentInGoalState;
	private boolean sink;
	private double enteringProb;

	public State(int ID, boolean sink, double enteringProb) {
		this.ID = ID;
		this.sink = sink;
		neighbors = new ArrayList<State>();
		agentInGoalState = false;
		this.enteringProb = enteringProb == 0 ? 1.0 : enteringProb;
	}
	
	public int getID() {
		return ID;
	}

	public Agent getAgent() {
		return sink ? null : agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public boolean isEmpty() {
		return agent == null || sink;
	}

	public List<State> getNeighbors() {
		return neighbors;
	}
	
	public boolean isSink() {
		return sink;
	}
	
	public double getEnteringProb() {
		return enteringProb;
	}

	public void addNeighbor(State neighbor) {
		neighbors.add(neighbor);
	}
	
	public void removeNeighbor(State neighbor) {
		neighbors.remove(neighbor);
	}
	
	public String getNeighborsToString(){
		StringBuffer sb = new StringBuffer();
		for (State s : neighbors) {
			sb.append(s.getID() + " ");
		}
		return sb.toString();
	}
	
	public void agentAchievedGoalState(){
		agentInGoalState = true;
	}
	
	public void resetGoalState(){
		agentInGoalState = false;
	}
	
	public String toString(){
		return "State: " + getID();
	}
	
	/*
	 * return true if agent is in his goal state
	 */
	public boolean isAgentInGoalState(){
		return sink ? false : agentInGoalState;
	}
}